package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.util.DBUtil;
import sun.security.pkcs11.Secmod;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressDaoImpl implements DeliveryAddressDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) throws Exception {
        List<Deliveryaddress> list = new ArrayList<>();
        String sql = "select * from deliveryAddress where userId = ?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while (rs.next()){
                Deliveryaddress deliveryaddress = new Deliveryaddress();
                deliveryaddress.setDaId(rs.getInt("daId"));
                deliveryaddress.setContactName(rs.getString("contactName"));
                deliveryaddress.setContactSex(rs.getInt("contactSex"));
                deliveryaddress.setContactTel(rs.getString("contactTel"));
                deliveryaddress.setAddress(rs.getString("address"));
                deliveryaddress.setUserId(rs.getString("userId"));
                list.add(deliveryaddress);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }

    @Override
    public Deliveryaddress getDeliveryAddressById(int daId) throws Exception {
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        String sql = "select * from deliveryAddress where daId = ?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            rs = pst.executeQuery();
            while (rs.next()){
                deliveryaddress = getDeliveryAddress(rs);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return deliveryaddress;
    }

    @Override
    public int saveDeliveryAddress(Deliveryaddress deliveryaddress) throws Exception {
        int result = 0;
        String sql = "insert into deliveryAddress(contactName,contactSex,contactTel,address,userId) value(?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            int count = 1;
            pst.setString(count++, deliveryaddress.getContactName());
            pst.setInt(count++, deliveryaddress.getContactSex());
            pst.setString(count++, deliveryaddress.getContactTel());
            pst.setString(count++, deliveryaddress.getAddress());
            pst.setString(count++, deliveryaddress.getUserId());
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int updateDeliveryAddress(Deliveryaddress deliveryaddress) throws Exception {
        int result = 0;
        String sql = "update deliveryAddress set contactName = ?,contactSex = ?,contactTel = ?,address = ?,userId = ? where daId = ?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            int count = 1;
            pst.setString(count++, deliveryaddress.getContactName());
            pst.setInt(count++, deliveryaddress.getContactSex());
            pst.setString(count++, deliveryaddress.getContactTel());
            pst.setString(count++, deliveryaddress.getAddress());
            pst.setString(count++, deliveryaddress.getUserId());
            pst.setInt(count++, deliveryaddress.getDaId());
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(int daId) throws Exception {
        int result = 0;
        String sql = "delete from deliveryAddress where daId = ?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    private Deliveryaddress getDeliveryAddress(ResultSet rs) throws Exception{
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        deliveryaddress.setDaId(rs.getInt("daId"));
        deliveryaddress.setContactName(rs.getString("contactName"));
        deliveryaddress.setContactSex(rs.getInt("contactSex"));
        deliveryaddress.setContactTel(rs.getString("contactTel"));
        deliveryaddress.setAddress(rs.getString("address"));
        deliveryaddress.setUserId(rs.getString("userId"));
        return deliveryaddress;
    }
}
